package com.service;

import com.mapper.ItemMapper;
import com.mapper.OptionMapper;
import com.mapper.SubjectMapper;
import com.mapper.UserMapper;
import com.entity.Option;
import com.entity.Subject;
import com.entity.UserInfo;
import com.entity.vo.SubjectVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
@Service
public class OptionService {
	@Resource
	private OptionMapper optionMapper;
	@Resource
	private ItemMapper itemMapper;
	@Resource
	private SubjectMapper subjectMapper;
	public List<Map> selectOptionDetail(Integer subid) {
		return this.optionMapper.selectOptionDetail(subid);
	}
	// ��ѯһ��option ����vsid
	public List<Option> selectOptionById(Integer vsid) {
		return this.optionMapper.selectOptionByVsId(vsid);
	}
	// ����opidɾ��option return 2 ����ɾ��
	public Integer deleteOption(Integer oid) {
		Integer opcount = this.itemMapper.selectItemByVoid(oid);
		if (opcount > 0) {// ��ѡ���Ѿ�͸��Ʊ�� ����ɾ��
			return 2;
		} else {
			// ����ɾ��
			return this.optionMapper.deleteOptionById(oid);
		}
	}
	@Transactional
	public Integer editOption(Integer[] ids, String[] ops, Subject sub) {
		System.out.println("opslength="+ops.length);
		// �޸ı���
		Integer substate = this.subjectMapper.updateSubject(sub);
		int i = 0;
		if (ids != null && ids.length > 0) {
			for (; i < ids.length; i++) { // 在ids有值的情况下 做修改操作
				String ocontent = ops[i];
				Integer oid = ids[i];
				Option o = new Option();
				o.setVo_id(oid);
				o.setVo_option(ocontent);
				o.setVs_id(sub.getVs_id());
				this.optionMapper.updateOption(o);
			}
		}
		// 在ids没有值的情况下, 而ops内容有值的情况下 做添加操作
		for (int w = i; w < ops.length; w++) {
			String ocontent = ops[i];
			Option o = new Option();
			o.setVo_option(ocontent);
			o.setVs_id(sub.getVs_id());
			this.optionMapper.saveOption(o);
		}
		return 1;
	}
}
